package com.example.yjh.zyfypt_314yjh.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yjh.zyfypt_314yjh.R;
import com.example.yjh.zyfypt_314yjh.activity.CollectActivity;
import com.example.yjh.zyfypt_314yjh.activity.FocusActivity;
import com.example.yjh.zyfypt_314yjh.activity.LoginActivity;
import com.example.yjh.zyfypt_314yjh.model.LogoutModel;

import static android.content.Context.MODE_PRIVATE;


/**
 * Created by YJH on 2017/4/9.
 */

public class Fragment5 extends Fragment implements View.OnClickListener {
    private View view=null;
    private Context context;
    private TextView tvname;
    private Button btlogout,btcollect,btfocus;
    private  SharedPreferences sp;
    private String sessionID;
    private String realname;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        //动态加载Fragment3的布局文件
        view = inflater.inflate(R.layout.fragment5, container, false);
        sp = context.getSharedPreferences("login", MODE_PRIVATE);
        readRealname();
        readSessionID();
        init();
        tvname.setText(realname);
        //返回动态生成的view
        return view;
    }

    private void readRealname() {
        realname=sp.getString("realname",null);
    }
    private void readSessionID() {
        sessionID=sp.getString("sessionID",null);
    }
    private void init() {
        tvname=(TextView)view.findViewById(R.id.textView13);
        btlogout=(Button)view.findViewById(R.id.logoutbutton);
        btlogout.setOnClickListener(this);
        btcollect=(Button)view.findViewById(R.id.collectbutton);
        btcollect.setOnClickListener(this);
        btfocus=(Button)view.findViewById(R.id.focusbutton);
        btfocus.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.logoutbutton:
                LogoutModel logoutModel = new LogoutModel();
                //如果注销成功
                if (logoutModel.logout(sp.getString("sessionID", null))) {
                    Toast.makeText(context,"注销成功",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.collectbutton:
                Intent intent = new Intent(context, CollectActivity.class);
                Toast.makeText(context,"我的收藏",Toast.LENGTH_SHORT).show();
                startActivity(intent);
                break;
            case R.id.focusbutton:
                intent = new Intent(context, FocusActivity.class);
                Toast.makeText(context,"我的关注",Toast.LENGTH_SHORT).show();
                startActivity(intent);
                break;
        }
    }
}
